package com.petmall.controller;

import com.petmall.entity.*;
import com.petmall.service.CartService;
import com.petmall.service.MessageService;
import com.petmall.service.OrderService;
import com.petmall.util.SessionAttributes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;

/**
 * Created by pig2guang on 2017/4/20.
 */
@Controller
public class OrderController {

    @Autowired
    private OrderService orderservice;

    @Autowired
    private CartService cartservice;

    @Autowired
    private MessageService messageservice;

    @RequestMapping(value = "/creatOrder")
    public String creatOrder(@ModelAttribute(value = "order") Order order, HttpSession httpSession, Model model) {
        String order_id = orderservice.creatOrder(order, httpSession.getAttribute(new SessionAttributes().user));
        model.addAttribute("order", orderservice.getOrderByOrderId(order_id));
        return "pay";
    }

    @RequestMapping(value = "/makeOrder")
    public String makeOrder(HttpSession httpSession, Model model) {
        Order order = new Order();
        ArrayList<Cart_item_info> cart_items = cartservice.listAllItemsInCart(httpSession.getAttribute(new SessionAttributes().user));
        model.addAttribute("cart_items", cart_items);
        model.addAttribute(order);
        return "checkout-billing-details";
    }

    @RequestMapping(value = "/myUnpaidOrder")
    public String myUnpaidOrder(HttpSession httpSession, Model model) {
        ArrayList<Order_items> orders = orderservice.getUnpaidUserOrders(httpSession.getAttribute(new SessionAttributes().user));
        model.addAttribute("orders", orders);
        return "my-unpaid-order";
    }

    @RequestMapping(value = "/myUnsentOrder")
    public String myUnsentOrder(HttpSession httpSession, Model model) {
        ArrayList<Order_items> orders = orderservice.getUnSentUserOrders(httpSession.getAttribute(new SessionAttributes().user));
        model.addAttribute("orders", orders);
        return "my-unsent-order";
    }

    @RequestMapping(value = "/myUnreceivedOrder")
    public String myUnreceivedOrder(HttpSession httpSession, Model model) {
        ArrayList<Order_items> orders = orderservice.getUnreceivedUserOrders(httpSession.getAttribute(new SessionAttributes().user));
        model.addAttribute("orders", orders);
        return "my-unreceived-order";
    }

    @RequestMapping(value = "/myUnreviewedOrder")
    public String myUnreviewedOrder(HttpSession httpSession, Model model) {
        ArrayList<Order_items> orders = orderservice.getUnreviewedUserOrders(httpSession.getAttribute(new SessionAttributes().user));
        model.addAttribute("orders", orders);
        return "my-unreviewed-order";
    }

    @RequestMapping(value = "/myCompletedOrder")
    public String myCompletedOrder(HttpSession httpSession, Model model) {
        ArrayList<Order_items> orders = orderservice.getCompletedUserOrders(httpSession.getAttribute(new SessionAttributes().user));
        model.addAttribute("orders", orders);
        return "my-completed-order";
    }

    @RequestMapping(value = "/payOrder")
    public String payOrder(@RequestParam("order_id") String order_id, HttpSession httpSession, Model model) {
        Order order = orderservice.getOrderByOrderId(order_id);
        model.addAttribute("order", order);
        return "pay";
    }

    @RequestMapping(value = "/paid", method = RequestMethod.GET)
    public String paid(@RequestParam("order_id") String order_id, HttpSession httpSession, Model model) {
        orderservice.updateUnpaidOrderToUnsent(order_id);
        ArrayList<Order_items> orders = orderservice.getUnSentUserOrders(httpSession.getAttribute(new SessionAttributes().user));
        model.addAttribute("orders", orders);
        return "my-unsent-order";
    }

    @RequestMapping(value = "/cancleOrder", method = RequestMethod.GET)
    public String cancleOrder(@RequestParam("order_id") String order_id, HttpSession httpSession, Model model) {
        orderservice.updateOrderStatusToCancled(order_id);
        messageservice.addCancleMessage(order_id, httpSession.getAttribute(new SessionAttributes().user));
        ArrayList<Order_items> orders = orderservice.getUnpaidUserOrders(httpSession.getAttribute(new SessionAttributes().user));
        model.addAttribute("orders", orders);
        return "my-unpaid-order";
    }

    @RequestMapping(value = "/receiveOrder", method = RequestMethod.GET)
    public String receiveOrder(@RequestParam("order_id") String order_id, HttpSession httpSession, Model model) {
        orderservice.updateUnreceivedOrderToUnreviewed(order_id, httpSession.getAttribute(new SessionAttributes().user));
        messageservice.addReviewMessage(order_id, httpSession.getAttribute(new SessionAttributes().user));
        ArrayList<Order_items> orders = orderservice.getUnreviewedUserOrders(httpSession.getAttribute(new SessionAttributes().user));
        model.addAttribute("orders", orders);
        return "my-unreviewed-order";
    }

    @RequestMapping(value = "/reviewOrder", method = RequestMethod.GET)
    public String reviewOrder(@RequestParam("order_id") String order_id, HttpSession httpSession, Model model) {
        model.addAttribute("order_id", order_id);
        model.addAttribute("review", new Review());
        model.addAttribute("items", orderservice.listOrderItemToReview(order_id));
        return "review";
    }

    @RequestMapping(value = "/reviewOrderItem", method = RequestMethod.GET)
    public String reviewOrderItem(@RequestParam("score") String score, @RequestParam("detail") String detail, @RequestParam("o_i_id") String order_item_id, @RequestParam("i_id") String item_id, HttpSession httpSession, Model model) {
        String order_id = orderservice.reviewOrderItem(score,detail,order_item_id,item_id,httpSession.getAttribute(new SessionAttributes().user));
        model.addAttribute("order_id", order_id);
        model.addAttribute("review", new Review());
        model.addAttribute("items", orderservice.listOrderItemToReview(order_id));
        return "review";
    }

    @RequestMapping(value = "/orderManagement", method = RequestMethod.GET)
    public String orderManagement(HttpSession httpSession, Model model) {
        if (httpSession.getAttribute(new SessionAttributes().admin) != null) {
            model.addAttribute("orders", orderservice.getAllOrders());
            return "order-management";
        } else {
            model.addAttribute("admin", new Admin());
            model.addAttribute("user", new User());
            return "login";
        }
    }

    @RequestMapping(value = "/orderDetail", method = RequestMethod.GET)
    public String orderDetail(@RequestParam("order_id") String order_id, HttpSession httpSession, Model model) {
        if (httpSession.getAttribute(new SessionAttributes().admin) != null) {
            model.addAttribute("items", orderservice.getOrderItemsInfo(order_id));
            model.addAttribute("order", orderservice.getOrderByOrderId(order_id));
            return "order-detail";
        } else {
            model.addAttribute("admin", new Admin());
            model.addAttribute("user", new User());
            return "login";
        }
    }

    @RequestMapping(value = "/sendOrder")
    public String sendOrder(@ModelAttribute(value = "order") Order order, Model model) {
        orderservice.sendOrder(order);
        model.addAttribute("orders", orderservice.getAllOrders());
        return "order-management";

    }

}
